package com.yanqu.road.server.cmd.activity.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_CROSS_ACTIVITY_CHAT_LIST, desc = "拉取跨服活动聊天列表")
public class CrossActivityChatListCmd extends AbstractCommand {

	private int activityId;
	private List<Integer> paramList = new ArrayList<>();

	private int param2;

	private int groupId;
	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossActivityProto.CrossActivityChatListReqMsg reqMsg = CrossActivityProto.CrossActivityChatListReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		paramList = reqMsg.getParamList();
		param2 = reqMsg.getParam2();
		if(reqMsg.hasGroupId()){
			groupId = reqMsg.getGroupId();
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		//上跨服拉聊天数据
		CrossActivityProto.CrossActivityChatListReqMsg.Builder msg = CrossActivityProto.CrossActivityChatListReqMsg.newBuilder();
		msg.setActivityId(activityId);
		msg.addAllParam(paramList);
		msg.setParam2(param2);
		msg.setGroupId(groupId);
		ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(msg.getActivityId());
		if(null == activityInfo){//活动不在了
			activityInfo = TradeWarActivityMgr.getOpenActivityInfo(msg.getActivityId());
		}
		if(null == activityInfo){//活动不在了
			activityInfo = OreWarActivityMgr.getOpenActivityInfo(msg.getActivityId());
		}
		if(activityInfo == null){
			activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
		}
		if(activityInfo == null){
			activityInfo = StrongholdActivityMgr.getOpenActivityInfo(activityId);
		}
		YanQuMessage pbMsg;
		int code = 0;
		if(null != activityInfo && activityInfo.getType() == eActivityType.CrossTradeWarActivity.getValue()){
			pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_TRADE_WAR_CHAT_LIST, msg);
			code = Protocol.C_CROSS_TRADE_WAR_CHAT_LIST;
		}else if(null != activityInfo && activityInfo.getType() == eActivityType.OreWarActivity.getValue() && activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
			pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ORE_WAR_CHAT_LIST, msg);
			code = Protocol.C_CROSS_ORE_WAR_CHAT_LIST;
		} else if (null != activityInfo && activityInfo.getType() == eActivityType.TongBingActivity.getValue()) {
			pbMsg = YanQuMessageUtils.buildMessage(Protocol.C2_TONG_BING_GET_CHAT, msg);
			code = Protocol.C2_TONG_BING_GET_CHAT;
		} else if (null != activityInfo && activityInfo.getType() == eActivityType.ChuHanZB.getValue()) {
			pbMsg = YanQuMessageUtils.buildMessage(Protocol.C2_CHUHAN_GET_CHAT, msg);
			code = Protocol.C2_CHUHAN_GET_CHAT;
		} else if (null != activityInfo && activityInfo.getType() == eActivityType.TonBingActivity.getValue()) {
			pbMsg = YanQuMessageUtils.buildMessage(Protocol.C2_TON_BING_GET_CHAT, msg);
		} else if (null != activityInfo && activityInfo.getType() == eActivityType.ChannelTonBingActivity.getValue()) {
			player.sendChannelPlayerPacket(Protocol.N_TON_BING_GET_CHAT, msg);
			return;
		} else if (null != activityInfo && activityInfo.getType() == eActivityType.XiuXian.getValue()) {
			pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_XIU_XIAN_GET_CHAT, msg);
			code = Protocol.C_CROSS_XIU_XIAN_GET_CHAT;
		} else if (null != activityInfo && activityInfo.getType() == eActivityType.FiveHero.getValue()) {
			int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
			GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_FIVE_HERO_GET_CHAT, msg, player.getUserId(), activityId, groupId);
			return;
		} else {
			pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_ACTIVITY_CHAT_LIST, msg);
			//	code = Protocol.C_CROSS_ACTIVITY_CHAT_LIST;
		}

		if (player != null && activityInfo != null &&
				(activityInfo.getType() == eActivityType.TongBingActivity.getValue()
						|| activityInfo.getType() == eActivityType.ChuHanZB.getValue())) {
			// 特殊处理：部分商会分组活动走自己的聊天
			player.sendUnionActivityGroupPacket(code, msg, activityId);
		} else if (activityInfo != null && UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
			int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
			if (groupId < 0) {
				groupId = 0;
			}
			msg.setGroupId(groupId);
			GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_ACTIVITY_GROUP_CHAT_LIST, msg, player.getUserId(), activityId, groupId);
		}else {
			GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
		}
	}

	@Override
	public void sendData(GamePlayer player) {

	}
}
